﻿using System;
using System.IO;
using BDShop.Business.Helpers;
using BDShop.Configuration;

namespace BDShop.Business.Cache
{
    public class CacheManager
    {

        public static void ResetCache(CacheEnum cacheType, bool isFO = false)
        {
            string filePath;

            if (isFO)
            {
                filePath = GetFilePathFO(cacheType);
                var result = ResetCache(filePath);
                if (result)
                {
                    Logger.DefaultLogger.Error("Reset cache " + cacheType);
                }
            }
            else
            {
                filePath = GetFilePath(cacheType);
                ResetCache(filePath);
            }
        }

        public static bool ResetCache(string filePath)
        {
            try
            {

                if (!File.Exists(filePath))
                {
                    File.Create(filePath).Dispose();
                }

                using (var file = new StreamWriter(filePath))
                {
                    file.WriteLine(DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss"));
                }
                return true;
            }
            catch (Exception ex)
            {
                Logger.DefaultLogger.Error(ex);
                return false;
            }
        }

        /// <summary>
        /// Get đường dẫn file
        /// </summary>
        /// <param name="cacheType"></param>
        /// <returns></returns>
        public static string GetFilePath(CacheEnum cacheType)
        {
            return Path.Combine(BDShopConfiguration.Storage.CachePathBO, cacheType + ".txt");
            //            return string.Format("{0}\\Caches\\{1}.txt", LiuLoConfiguration.Storage.CachePathBO, cacheType.ToString());
        }

        public static string GetFilePathFO(CacheEnum cacheType)
        {
            return Path.Combine(BDShopConfiguration.Storage.CachePathFO, cacheType + ".txt");
            //            return string.Format("{0}\\Caches\\{1}.txt", LiuLoConfiguration.Storage.CachePathBO, cacheType.ToString());
        }

        public static string GetFilePath(string name)
        {
            return Path.Combine(BDShopConfiguration.Storage.CachePathBO, name + ".txt");
            //            return string.Format("{0}\\Caches\\{1}.txt", LiuLoConfiguration.Storage.CachePathBO, cacheType.ToString());
        }
    }
}